﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using StandardDAC.Base;
using StandardDAC;
using MySql.Data.MySqlClient;

namespace MySqlDAC {
    /// <summary>
    /// Adapter for SQL Server connections
    /// </summary>
    internal class MySqlDbTypeAdapter : IDBTypeAdapter {
        #region IDBType Members
        /// <summary>
        /// Creates a new MySqlCommand with the given connection and command text
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="connection"></param>
        /// <param name="conn"></param>
        /// <returns></returns>
        public DbCommand NewCommand(string commandText, DbConnection connection, ConnOrTranInfo conn) {
            MySqlCommand cmd = new MySqlCommand(commandText, (MySqlConnection)connection);
            if (conn is TranInfo)
                cmd.Transaction = (MySqlTransaction)((TranInfo)conn).Transaction;
            return cmd;
        }
        /// <summary>
        /// Creates a new MySqlCommand.
        /// </summary>
        /// <returns></returns>
        public DbCommand NewCommand() {
            return new MySqlCommand();
        }
        /// <summary>
        /// Creates a new MySqlConnection
        /// </summary>
        /// <param name="conn">connection or transaction</param>
        /// <returns></returns>
        public DbConnection NewConnection(ConnOrTranInfo conn) {
            if (conn is ConnInfo) {
                MySqlConnection c = new MySqlConnection(((ConnInfo)conn).ConnectionString);
                c.Open();
                return c;
            } else
                return ((TranInfo)conn).Transaction.Connection;
        }
        /// <summary>
        /// Creates a new MySqlDataAdapter for the given MySqlCommand
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public DbDataAdapter NewAdapter(DbCommand command) {
            return new MySqlDataAdapter((MySqlCommand)command);
        }
        /// <summary>
        /// Adds parameter of the given type and name to the command object
        /// </summary>
        /// <param name="name">parameter name</param>
        /// <param name="dbType">parameter type</param>
        /// <param name="command">command to add the parameter to</param>
        /// <returns></returns>
        public DbParameter AddNewParameter(string name, object dbType, DbCommand command) {
            return ((MySqlCommand)command).Parameters.Add(name, (MySqlDbType)dbType);
        }
        /// <summary>
        /// Not needed. Does nothing
        /// </summary>
        /// <param name="command"></param>
        public void SortParametersIfNeeded(Dictionary<string, KeyValuePair<int, DBColumn>> parametersToSort, DbCommand cmd) {
        }
        /// <summary>
        /// Adds a parameter, ignoring is type, with a null value.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        public DbParameter AddNewNullParameter(string name, DbCommand command) {
            return ((MySqlCommand)command).Parameters.AddWithValue(name, DBNull.Value);
        }
        /// <summary>
        /// Adds an existing parameter back on to the command
        /// </summary>
        /// <param name="param"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        public DbParameter AddExistingParameter(DbParameter param, DbCommand command) {
            return ((MySqlCommand)command).Parameters.Add((MySqlParameter)param);
        }
        /// <summary>
        /// Returns parameter name from column name
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public string ConvertNameToParam(string columnName) {
            return "@" + columnName;
        }
        /// <summary>
        /// Not needed. Does nothing
        /// </summary>
        /// <param name="command"></param>
        public void PreExecute(DbCommand command) {
        }
        /// <summary>
        /// Not needed. Does nothing
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="type"></param>
        public void AddOutputCursorIfNeeded(DbCommand cmd, Type type) {
        }

        #endregion
    }
}
